from flask import Flask
import os


def create_app():
    # 实例
    app = Flask(__name__)

    # 配置
    from config import BaseConfig, ProdConfig, DevConfig
    if 'FLASK_ENV' not in os.environ:
        app.config.from_object(BaseConfig)
    else:
        app.config.from_object(DevConfig) if os.environ['FLASK_ENV'] == 'development' else app.config.from_object(
            ProdConfig)

    # 跨域
    from flask_cors import CORS
    CORS(app, resources={r"/api/*": {"origins": "http://127.0.0.1:8080"}}, supports_credentials=True)

    # 数据库
    from Plugins.database import db, migrate
    db.init_app(app)
    migrate.init_app(app, db)

    # CSRF
    from Plugins.csrf import Csrf
    Csrf(app)

    # 接口
    from Common.Interface import reg_interface
    reg_interface(app)

    # 登录
    from Plugins.login import login_manager
    login_manager.init_app(app)

    # 注册蓝图
    from Plugins.blueprint import reg_blueprint
    reg_blueprint(app)

    # 注册模型
    import Plugins.RegTable

    return app
